import axios from "axios";
import router from '../router'
import {curUrl1,curUrl2} from 'util/prodUrl'
 
export function request(config) {
  const instance = axios.create({
    baseURL: curUrl1,
    timeout: 5000
  })

  instance.interceptors.request.use(res => {
    if (config.url != '/platform/pl/login') {
      res.headers.token = localStorage.getItem("token")
    }
    return res
  }, err => {
    console.log(err);
  })

  instance.interceptors.response.use(res => {
    return res.data
  }, err => {
    console.log(err.response);
    if(err.response.status == 401 || err.response.status == 500){
      localStorage.clear();     //删除用户信息
      // alert('token失效,请重新登录!');
      // 要跳转登陆页
      window.location.href = curUrl2 + '/back?back=back'
    }
  })

  return instance(config)
}